package cn.jluzh.blog.modules.user.service.impl;

import cn.jluzh.blog.mapper.SysUserMapper;
import cn.jluzh.blog.modules.user.entity.SysUser;
import cn.jluzh.blog.modules.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: hjc
 **/
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements UserService {
    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public String validAccount(String account) {
        String name = sysUserMapper.validAccount(account);
        String msg;
        if(StringUtils.isEmpty(name)){
            msg = "该账号可以使用";
        }else {
            msg = "该账号已被使用,请重新输入";
        }
        return msg;
    }

    @Override
    public SysUser getUserByAccount(String account) {
        return sysUserMapper.getUserByAccount(account);
    }

    @Override
    public String updatePwd(Integer id, String oldPass, String newPass) {
        String pwdById = this.getPwdById(id);
        String msg = null;
        // 如果输入的旧密码和根据id找到的密码相同
        if (pwdById.equals(oldPass)){
            sysUserMapper.updatePwd(id,oldPass,newPass);
            msg = "修改密码成功";
        }else{
            msg = "原密码有误，请重新输入";
        }
        return msg;
    }

    @Override
    public String getPwdById(Integer id) {
        String pwdById = sysUserMapper.getPwdById(id);
        return pwdById;
    }

    @Override
    public List<SysUser> getAllUserByName(String query) {
        return sysUserMapper.getAllUserByName(query);
    }

    @Override
    public String validEmail(String email) {
        String name = sysUserMapper.validEmail(email);
        String msg;
        if(StringUtils.isEmpty(name)){
            msg = "该邮箱可以使用";
        }else {
            msg = "该邮箱已被注册,请重新输入";
        }
        return msg;
    }
}
